package com.spboot.exts.service.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spboot.exts.entity.User;
import com.spboot.exts.entity.util.PageInfo;
import com.spboot.exts.mapper.UserMapper;
import com.spboot.exts.service.UserService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author test
 * @since 2019-08-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Autowired
	StringRedisTemplate redisTemplate;

	
//	@Cacheable(value = "userlist",key="1")
	public PageInfo<User> selectUserPage(PageInfo<User> paramIPage, Wrapper<User> paramWrapper) {
		PageInfo<User> page;
		// userlist_1
		// userlist_2
		String key = "userlist";
		String hk = paramIPage.getCurrent() + "";
		//判断
		Boolean hasCurrent = redisTemplate.opsForHash().hasKey(key,hk);
		
		System.out.println(hasCurrent);
		if(!hasCurrent){
			//没有
			page = (PageInfo<User>) this.baseMapper.selectUserPage(paramIPage, paramWrapper);
			
			//添加到redis
			redisTemplate.opsForHash().put(key,hk, JSON.toJSONString(page));
		}else{
			String json = redisTemplate.opsForHash().get(key,hk).toString();
			System.out.println(json);
			page = JSON.parseObject(json,PageInfo.class);
		}
		return page;
//		return (PageInfo<User>) this.baseMapper.selectUserPage(paramIPage, paramWrapper);
	}

	public Set<String> selectUserPermission(Long id) {
		return this.baseMapper.selectUserPermission(id);
	}

}
